/**
 * 路由定义
 * transitionName: 路由切换过渡动画名称
 */
import { createRouter, createWebHashHistory } from 'vue-router';
import { ElMessage } from 'element-plus';
import NProgress from 'nprogress';
import { isLogin } from '@/utils/auth';
import route from './routes.js';

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    ...route,
    // 登录
    {
      path: '/login',
      name: 'Login',
      meta: {
        title: '登录',
        needLogin: false,
        transitionName: 'router',
      },
      component: () => import(/* webpackChunkName: "Login" */ '@/pages/module-login/index.vue'),
    },
    // 系统管理
    {
      path: '/base-system',
      name: 'BaseSystem',
      redirect: '/base-role',
      meta: {
        title: '系统管理',
        needLogin: true,
        transitionName: 'router',
      },
      component: () => import(/* webpackChunkName: "BaseSystem" */ '@/pages/module-base-system/index.vue'),
      children: [
        {
          path: '/base-menu',
          name: 'baseMenu',
          meta: {
            title: '系统管理-菜单管理',
            name: '菜单管理',
            icon: '菜单管理_icon',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('@/pages/module-base-system/base-menu/index.vue'),
        },
        {
          path: '/base-role',
          name: 'baseRole',
          meta: {
            title: '系统管理-角色管理',
            name: '角色管理',
            icon: '角色管理_icon',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import(/* webpackChunkName: "baseRole" */ '@/pages/module-base-system/base-role/index.vue'),
        },
        {
          path: '/base-power',
          name: 'basePower',
          meta: {
            title: '系统管理-权限管理',
            name: '权限管理',
            icon: '权限管理_icon',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import('@/pages/module-base-system/base-power/index.vue'),
        },
        {
          path: '/base-user',
          name: 'baseUser',
          meta: {
            title: '系统管理-用户管理',
            name: '用户管理',
            icon: '用户管理_icon',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import(/* webpackChunkName: "baseUser" */ '@/pages/module-base-system/base-user/index.vue'),
        },

        // {
        //   path: '/base-dictionary',
        //   name: 'baseDictionary',
        //   meta: {
        //     title: '系统管理-字典管理',
        //     name: '字典管理',
        //     icon: 'dict',
        //     needLogin: true,
        //     transitionName: 'router',
        //   },
        //   component: () => import('@/pages/module-base-system/base-dictionary/index.vue'),
        // },
        {
          path: '/base-organization',
          name: 'baseOrganization',
          meta: {
            title: '系统管理-组织管理',
            name: '组织管理 ',
            icon: '组织管理_icon',
            needLogin: true,
            transitionName: 'router',
          },
          component: () =>
            import(/* webpackChunkName: "baseOrganization" */ '@/pages/module-base-system/base-organization/index.vue'),
        },
        {
          path: '/base-log',
          name: 'baseLog',
          meta: {
            title: '系统管理-日志管理',
            name: '日志管理',
            icon: '日志管理_icon',
            needLogin: true,
            transitionName: 'router',
          },
          component: () => import(/* webpackChunkName: "baseLog" */ '@/pages/module-base-system/base-log/index.vue'),
        },

        {
          path: '/base-layer-data',
          name: 'baseLayerData',
          meta: {
            title: '系统管理-图层配置',
            name: '图层配置 ',
            icon: '图层配置_icon',
            needLogin: true,
            transitionName: 'router',
          },
          component: () =>
            import(/* webpackChunkName: "baseLayerData" */ '@/pages/module-base-system/base-layer-data/index.vue'),
        },
      ],
    },
    {
      path: '/:pathMatch(.*)',
      name: '404',
      meta: {
        title: '404',
        needLogin: false,
        transitionName: 'router',
      },
      component: () => import(/* webpackChunkName: "base404" */ '@/components/Router404.vue'),
    },
  ],
});

router.beforeEach((to, from, next) => {
  NProgress.start();

  // 如果需要登录
  if (to.meta.needLogin) {
    // 如果有token 则直接放行
    if (isLogin()) {
      next();
    } else {
      // 否则去登录页
      ElMessage.error('请先登录！');
      next('/login');
    }
  } else {
    // 不需要登录则直接放行
    next();
  }
});

// 修改标题的工作可以放在全局后置守卫
router.afterEach((to) => {
  setTimeout(() => {
    NProgress.done();
  }, 200);

  if (to.meta.title) {
    document.title = `${import.meta.env.VITE_APP_NAME} - ${to.meta.title}`;
  }
});

export default router;
